﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Xml.Serialization;

namespace IndustrialCommunication.Common
{
    public class SerializeConvert
    {
        /// <summary>
        /// 二进制序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] BinarySerialize(object obj)
        {
            using (var stream=new MemoryStream())
            {
                var formatter = new BinaryFormatter();
                formatter.Serialize(stream, obj);
                return stream.ToArray();
            }
        }
        /// <summary>
        /// 二进制序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static T BinaryDeserialize<T>(byte[] data,int offset,int length)
        {
            using (var stream = new MemoryStream(data,offset,length))
            {
                stream.Position = 0;
                var formatter = new BinaryFormatter();
                return (T)formatter.Deserialize(stream);
            }
        }
        /// <summary>
        /// 二进制反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static T BinaryDeserialize<T>(byte[] data)
        {
            using (var stream = new MemoryStream(data))
            {
                stream.Position = 0;
                var formatter = new BinaryFormatter();
                return (T)formatter.Deserialize(stream);
            }
        }
        /// <summary>
        /// XML序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static byte[] XmlSerialize<T>(T obj)
        {
            var serializer = new XmlSerializer(typeof(T));
            using (var stream=new MemoryStream())
            {
                serializer.Serialize(stream, obj);
                return stream.ToArray();
            }
        }
        /// <summary>
        /// XML反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static T XmlDeserialize<T>(byte[] data,int offset,int length)
        {
            var serializer = new XmlSerializer(typeof(T));
            using (var stream = new MemoryStream(data,offset,length))
            {
                stream.Position = 0;
                return (T)serializer.Deserialize(stream);
            }
        }
        /// <summary>
        /// XML反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static T XmlDeserialize<T>(byte[] data)
        {
            var serializer = new XmlSerializer(typeof(T));
            using (var stream=new MemoryStream(data))
            {
                stream.Position = 0;
                return (T)serializer.Deserialize(stream);
            }
        }
        /// <summary>
        /// Json序列化
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
         public static byte[] JsonSerialize(object obj)
        {
            return Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(obj));
        }
        /// <summary>
        /// Json反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <param name="offset"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static T JsonDeserialize<T>(byte[] data,int offset,int length)
        {
            var str = Encoding.UTF8.GetString(data,offset,length);
            return JsonConvert.DeserializeObject<T>(str);
        }
        /// <summary>
        /// Json反序列化
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="data"></param>
        /// <returns></returns>
        public static T JsonDeserialize<T>(byte[] data)
        {
            var str = Encoding.UTF8.GetString(data);
            return JsonConvert.DeserializeObject<T>(str);
        }
    }
}
